{-- Pitch Dynamics - version 1.1 - by Frank Condello http://chaoticbox.com/ --}

{ Parameters:
  
	Controller : The MIDI controller number sending the positional data.

	    Detune : The tuning offset for the edge of the drum (in semitones). To lower the
				 pitch rather than raise it, activate the Invert_Detune button directly
				 below this knob.

	      Zone : Adjusts the "dead zone" in the centre of the drum where no pitch modulation
				 should occur. 0 means only hits dead centre will be natural pitch, and 126
				 means only hits on the extreme edge will modify the pitch. Note that this
				 parameter only affects the controller based pitch, if does not affect
				 VeloDet.

	   VeloDet : If non-zero, harder hits will pitch towards this tuning offset (in semitones).
				 A velocity of 1 equals no change, and 127 is full VeloDet offset. To lower the
				 pitch rather than raise it, activate the Invert_VeloDet button directly below
				 this knob.

Limiting the affected note range:

 Press the "Set_Note_Range" button and follow the prompts by hitting a key on the Kontakt
 graphical keyboard, or attached MIDI hardware to set the desired key range. The "Low" and
 "High" note values can also be manipulated directly.
}

on init
	
	declare ui_value_edit $Controller (0, 127, 1)
	declare ui_knob $Detune (0, 1200, 100)
	declare ui_value_edit $Zone (0, 126, 1)
	declare ui_knob $VeloDet (0, 1200, 100)
	declare ui_button $Invert_Detune
	declare ui_button $Invert_VeloDet
	$Controller := 1
	$Detune := 200
	$Zone := 20
	$VeloDet := 0
	make_persistent ($Controller)
	make_persistent ($Detune)
	make_persistent ($Zone)
	make_persistent ($VeloDet)
	make_persistent ($Invert_Detune)
	make_persistent ($Invert_VeloDet)
	declare polyphonic $tuneAmount
	declare polyphonic $ccAmount
	declare polyphonic $velZoneRatio
	declare polyphonic $zoneScaled
	declare polyphonic $direction
	move_control($Controller, 1, 1)
	move_control($Detune, 2, 1)
	move_control($Zone, 1, 2)
	move_control($VeloDet, 3, 1)
	move_control($Invert_Detune, 2, 3)
	move_control($Invert_VeloDet, 3, 3)

	{ Note Ranger }
	declare ui_value_edit $Low (0, 127, 1)
	declare ui_value_edit $High (0, 127, 1)
	declare ui_button $Set_Note_Range
	declare ui_label $RangeStateDisplay (1, 1)
	$High := 127
	set_text($RangeStateDisplay, "")
	make_persistent ($Low)
	make_persistent ($High)
	declare $setRangeState
	declare const $setRangeOff := 0
	declare const $setRangeLow := 1
	declare const $setRangeHigh := 2
	move_control($Low, 5, 2)
	move_control($High, 6, 2)
	move_control($Set_Note_Range, 5, 1)
	move_control($RangeStateDisplay, 6, 1)

	message (" ")
	
end on

on ui_control($Set_Note_Range)

	if ( $Set_Note_Range = 1 )
		$setRangeState := $setRangeLow
		set_text ($RangeStateDisplay, "Hit Low Note")
	else
		$setRangeState := $setRangeOff
		set_text ($RangeStateDisplay, "")
	end if

end on

on note

	{-- Range setting mode... --}
	
	if ( $setRangeState # $setRangeOff and $Set_Note_Range = 1 )
		if ( $setRangeState = $setRangeLow )
			$Low := $EVENT_NOTE
			set_text ($RangeStateDisplay, "Hit High Note")
			$setRangeState := $setRangeHigh
		else
			$High := $EVENT_NOTE
			$Set_Note_Range := 0
			$setRangeState := $setRangeOff
			set_text ($RangeStateDisplay, "")
		end if
		exit
	end if
	
	if ( $EVENT_NOTE < $Low or $EVENT_NOTE > $High )
		exit
	end if
	
	{-- Playback mode... --}
	
	$tuneAmount := 0
	
	{ Grab tuning for CC }
	if ( $Detune # 0 and $Controller > 0 )
		$ccAmount := %CC[$Controller]
		if ( $ccAmount > $Zone )
			$ccAmount := $ccAmount*1000
			{ Factor in dead zone }
			if ( $Zone > 0 )
				$zoneScaled := $Zone*1000			
				$velZoneRatio := (127000-$zoneScaled)/126
				$ccAmount := 1000 * ( ($ccAmount-$zoneScaled)/$velZoneRatio )
			end if
			if ( $Invert_Detune = 0 )
				$tuneAmount := $Detune * ( $ccAmount/127 )
			else
				$tuneAmount := -$Detune * ( $ccAmount/127 )
			end if
		end if
	end if
	
	{ Add tuning for velocity }
	if ( $VeloDet # 0 and $EVENT_VELOCITY > 1 )
		if ( $Invert_VeloDet = 0 )
			$tuneAmount := $tuneAmount + ( $VeloDet * ( ($EVENT_VELOCITY*1000)/127 ) )
		else
			$tuneAmount := $tuneAmount + ( -$VeloDet * ( ($EVENT_VELOCITY*1000)/127 ) )
		end if
	end if
	
	if ( $tuneAmount # 0 )
		change_tune($EVENT_ID, $tuneAmount, 1)
	end if
	
end on
